package day02.service.Impl;

import day02.connection.UserConnection;
import day02.db.DataSource;
import day02.entity.User;
import day02.service.UserService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {

    private Connection connection = null;
    private PreparedStatement pstmt = null;

    public UserServiceImpl(DataSource dataSource) {
        connection = UserConnection.getConnection(dataSource);
    }

    public void setPstmt(String query) {
        try {
            this.pstmt = connection.prepareStatement(query);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean saveUser(User user) {
        try {
            setPstmt("insert into user(id, name, age, level) values(?, ?, ?, ?)");
            pstmt.setString(1, user.getId());
            pstmt.setString(2, user.getName());
            pstmt.setInt(3, user.getAge());
            pstmt.setInt(4, user.getLevel());
            int result = pstmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteUser(String id) {
        try {
            setPstmt("delete from user where id = ?");
            pstmt.setString(1, id);
            int result = pstmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updateUser(User user) {
        try {
            setPstmt("update user set name = ?, age = ?, level = ? where id = ?");
            pstmt.setString(1, user.getName());
            pstmt.setInt(2, user.getAge());
            pstmt.setInt(3, user.getLevel());
            pstmt.setString(4, user.getId());
            int result = pstmt.executeUpdate();
            return result > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public User getUserById(String id) {
        User user = null;
        try {
            setPstmt("select * from user where id = ?");
            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();
            user = new User();
            if(rs.next()) {
                user.setId(rs.getString(1));
                user.setName(rs.getString(2));
                user.setAge(rs.getInt(3));
                user.setLevel(rs.getInt(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<User> getUsers() {
        List<User> users = new ArrayList<>();
        try {
            setPstmt("select * from user");
            ResultSet rs = pstmt.executeQuery();

            while(rs.next()) {
                User user = new User();
                user.setId(rs.getString(1));
                user.setName(rs.getString(2));
                user.setAge(rs.getInt(3));
                user.setLevel(rs.getInt(4));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }
}
